package com.wd.lookcar

import android.widget.CompoundButton
import androidx.activity.viewModels
import androidx.appcompat.widget.AppCompatRadioButton
import androidx.recyclerview.widget.LinearLayoutManager
import com.wd.common.DataHelper
import com.wd.common.base.BaseActivity
import com.wd.common.bean.AllQuickSearchBean
import com.wd.common.viewmodel.MyViewModel
import com.wd.lookcar.adapter.ConditionAdapter
import com.wd.lookcar.databinding.ActivitySearchBinding
import org.greenrobot.eventbus.EventBus


class SearchActivity : BaseActivity<ActivitySearchBinding, Any>() {
    override fun getViewBinding() = ActivitySearchBinding.inflate(layoutInflater)
    private val viewModel : MyViewModel by viewModels()
    private lateinit var allQuickSearch : AllQuickSearchBean
    override fun initData() {
        viewModel.findAllCondition()
        binding.recParameter.layoutManager = LinearLayoutManager(this)
        viewModel.allQuickSear.observe(this){
            allQuickSearch = it
            for(index in 0 .. allQuickSearch.result.size){
                val radio = AppCompatRadioButton(this).apply {
                    text = allQuickSearch.result[index].conditionName
                    buttonDrawable = null
                }
                radio.setOnCheckedChangeListener{ compoundButton: CompoundButton, b: Boolean ->
                    binding.recParameter.adapter = ConditionAdapter(this,allQuickSearch.result[index].screeningConditionList)
                }
                if(index == 0){
                    radio.isChecked = true
                }
                binding.recCondition.addView(radio)
            }
        }
        binding.yes.setOnClickListener {
            viewModel.multiConditionQuery(1,20, DataHelper.map)
            viewModel.quickSearch.observe(this){
                EventBus.getDefault().postSticky(it)
                DataHelper.clearMap()
                finish()
            }
        }
    }

}